package com.vma.interfaceShoes.controller;

import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.PathKit;
import com.vma.interfaceShoes.util.ImageTool;
import com.vma.interfaceShoes.util.ReturnsUtil;
import com.vma.shoes.bizModel.model.User;
import com.vma.shoes.bizModel.model.UserImg;
import com.vma.user.service.UserService;
import net.sf.json.JSONObject;

import java.util.*;

/**
 * Created by chenzui on 2017/4/17.
 */
public class UserController extends Controller {
    public void getCode(){
        try{
            String phone = getPara("phone");
            renderJson(ReturnsUtil.returnSuccess(UserService.getSmsCode(phone)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }

    }

    public void signUp(){
        try{
            String phone = getPara("phone");
            String password = getPara("password");
            String smsMessageSid = getPara("smsMessageSid");
            String smsCode = getPara("smsCode");
            renderJson(ReturnsUtil.returnSuccess(UserService.signUp(phone,password,smsMessageSid,smsCode)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void modifyPhone(){
        try{
            String phone = getPara("phone");
            Integer userId = getParaToInt("userId");
            String smsMessageSid = getPara("smsMessageSid");
            String smsCode = getPara("smsCode");
            renderJson(ReturnsUtil.returnSuccess(UserService.modifyPhone(userId,phone,smsMessageSid,smsCode)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void login(){
        try{
            String phone = getPara("phone");
            String password = getPara("password");
            Map result = UserService.loginByPwd(phone,password);
            if("0".equals(result.get("resultCode").toString())){
                result.remove("resultCode");
                renderJson(ReturnsUtil.returnSuccess(result));
            }else {
                renderJson(ReturnsUtil.returnFail(result.get("resultMsg"),result.get("resultCode").toString()));
            }
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void login3rd(){
        try{
            String openId = getPara("openId");
            Integer userId = UserService.login3rd(openId);
            renderJson(ReturnsUtil.returnSuccess(userId));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void modifyPwd(){
        try{
            String old = getPara("old");
            Integer userId = getParaToInt("userId");
            String newPassword = getPara("newPassword");
            Map result = UserService.modifyPwd(userId,old,newPassword);
            if(result.get("errorCode").toString().equals("0")){
                renderJson(ReturnsUtil.returnSuccess("success"));
            }else {
                renderJson(ReturnsUtil.returnFail(result.get("errorInfo"),result.get("errorCode").toString()));
            }
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void getUserInfo(){
        try{
            Integer userId = getParaToInt("userId");
            renderJson(ReturnsUtil.returnSuccess(UserService.getUserInfo(userId)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }
    public void upload() {
        try {
            String imgData = getPara("imgdata");
            Integer userId = getParaToInt("userId");
            String imgName = generateWord() + ".jpg";
            String url = "/alidata01/chenz/www/Static_war_exploded" + "/headImg/" + imgName;
            ImageTool.generateImage(imgData, url);
            List<UserImg> imgs = UserImg.dao.find("SELECT * FROM user_img WHERE status = 1 AND user_id = ?",userId);
            if(imgs != null && imgs.size()>0){
                for(UserImg img:imgs){
                    img.setStatus("0").update();
                }
            }
            new UserImg().setStatus("1").setUserId(userId).setCreateTime(new Date()).setUrl("/headImg/"+imgName).save();

            renderJson(ReturnsUtil.returnSuccess("/headImg/"+imgName));

        } catch (Exception e) {
            renderJson(ReturnsUtil.returnException("upload", e));
        }
    }

    private String generateWord() {
        String[] beforeShuffle = new String[] { "2", "3", "4", "5", "6", "7",
                "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
                "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
                "W", "X", "Y", "Z" };
        List<String> list = Arrays.asList(beforeShuffle);
        Collections.shuffle(list);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
        }
        String afterShuffle = sb.toString();
        String result = afterShuffle.substring(5, 15);
        return result;
    }
}
